<template>
  <div class="overflow-hidden">
    <BasicTable @register="registerTable" style="margin-top: 20px">
      <template #toolbar>
        <a-button
          v-hasAnyPermission="['jiaoyan:upload']"
          preIcon="ant-design:download-outlined"
          type="primary"
          @click="handleBatchDownload"
        >
          {{ t('common.title.download') }}
        </a-button>
      </template>
    </BasicTable>
  </div>
</template>
<script lang="ts">
  import { defineComponent, reactive, ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { BasicTable, useTable } from '/@/components/Table';
  // import { RoleEnum } from '/@/enums/roleEnum';
  import { downloadFile, handleFetchParams } from '/@/utils/lamp/common';
  import { getTaskCheckCodeByYearAndType, JiaoyandownloadFile } from '/@/api/basic/qimo/index';
  import { columns } from './defDictItem.data';
  import { download } from '/@/api/devOperation/system/defFile';
  import { downloadFilejiaoyan } from '/@/utils/lamp/common';
  export default defineComponent({
    // 若需要开启页面缓存，请将此参数跟菜单名保持一致
    name: 'DefDictItemManagement',
    components: {
      BasicTable,
      // [Descriptions.name]: Descriptions,
      // [Descriptions.Item.name]: Descriptions.Item,
    },
    setup() {
      const { t } = useI18n();
      const { createMessage, createConfirm } = useMessage();
      // 编辑页弹窗

      const dictype = ref<string>('');
      const dicyear = ref<string>('');
      const dict = reactive<Recordable>({
        name: '',
        key: '',
        id: '',
      });

      // 表格
      const [registerTable, { reload, getSelectRowKeys, setTableData }] = useTable({
        title: '校验码列表',
        api: getTaskCheckCodeByYearAndType,
        columns: columns(),
        immediate: false,
        useSearchForm: false,
        beforeFetch: handleFetchParams,
        // searchInfo: {
        //   type: dictype.value,
        //   year: dicyear.value,
        // },
        searchInfo: {
          type: dictype,
          year: dicyear,
        },
        showTableSetting: true,
        bordered: true,
        rowKey: 'id',
        rowSelection: {
          type: 'checkbox',
          columnWidth: 40,
        },
      });
      function fetch(row: Recordable) {
        if (row) {
          dictype.value = row?.type as string;
          dicyear.value = row?.year as string;
          reload();
        } else {
          dictype.value = '';
          dicyear.value = '';
          setTableData([]);
          // reload();
        }
      }
      // 弹出复制页面
      // function handleCopy(record: Recordable, e: Event) {
      //   e?.stopPropagation();
      //   record.parentId = dictId.value;
      //   openDrawer(true, {
      //     record,
      //     type: ActionEnum.COPY,
      //     parent: toRaw(dict),
      //   });
      // }

      async function handleBatchDownload() {
        const response = await JiaoyandownloadFile([dicyear.value], [dictype.value]);

        downloadFilejiaoyan(response);

        createMessage.success(t('common.tips.downloadSuccess'));
      }

      return {
        t,
        handleBatchDownload,
        dict,
        fetch,
        registerTable,
        // RoleEnum,
      };
    },
  });
</script>
<style scoped lang="less">
  @prefix-cls: ~'@{namespace}-basic-table';

  .@{prefix-cls} {
    &-form-container {
      padding: 16px 16px 16px 0;
    }
  }
</style>
